<?php

namespace App\Providers;

use App\Facades\DeviceService;
use App\Facades\UserService;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
use Sentinel;
use Validator;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot(Request $request)
    {
        Validator::extend('telephone', function($attribute, $value, $parameters){
            return preg_match( config( 'regex.telephone' ), $value );
        });
        Validator::extend('id_number', function($attribute, $value, $parameters){
            return preg_match( config( 'regex.id_number' ), $value );
        });

        Validator::extend('device_code', function($attribute, $value, $parameters){
            return preg_match( config( 'regex.device_code' ), $value );
        });

        if($request->is('admin/*') && !$request->is('admin/api/*')){
            view()->composer('admin/*',function($view){
                $view->with([
                    'user' => Sentinel::getUser(),
                    'flow_reminders_count' => DeviceService::countFlowRemindListByStatus(0),
                    'filter_reminders_count' => DeviceService::countFilterRemindListByStatus(0),
                    'maintain_info_count' => DeviceService::countmaintainInfoListByStatus(0),
                    'after_services_count' => UserService::countAfterServiceListByStatus(0),
                    ]);
            });
        }
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
